package tms.controler;

import tms.model.Booking;
import tms.model.Flight;
import tms.view.SummaryPanel;

/**
 *  Handles ticket's information.
 */
public class SummaryController implements IController {

    private SummaryPanel view;
    private Booking departTicket;
    private Booking returnTicket;

    public SummaryController(Booking departTicket, Booking returnTicket) {
        this.departTicket = departTicket;
        this.returnTicket = returnTicket;
    }

    public Booking getDepatureTicket() {
        return departTicket;
    }

    public void setDepatureTicket(Booking departTicket) {
        this.departTicket = departTicket;
    }

    public Booking getReturnTicket() {
        return returnTicket;
    }

    public void setReturnTicket(Booking returnTicket) {
        this.returnTicket = returnTicket;
    }

    public void bindSummaryInfo(Booking departTicket, Booking returnTicket) {
        // needs departTicket not be null for getting TicketClass info
        if (departTicket != null) {
            String ticketClass = departTicket.getTicketClass();
            double dPrice = bindDepartInfo(departTicket, ticketClass);
            double rPrice = (returnTicket != null ? bindReturnInfo(returnTicket, ticketClass) : 0.0);
            bindTicketClass(ticketClass);
            bindGrandTotal(dPrice + rPrice);
        }
    }

    public void bindTicketClass(String ticketClass) {
        view.setTicketClass(ticketClass);
    }

    public void bindGrandTotal(double total) {
        view.setGrandTotal(total);
    }

    public double bindDepartInfo(Booking departTicket, String ticketClass) {
        assert (departTicket != null);
        Flight departFlight = departTicket.getFlight();
        double dPrice = 0.0;
        if (ticketClass.toLowerCase().startsWith("bus")) {
            dPrice = departFlight.getPrice2();
        } else {
            dPrice = departFlight.getPrice();
        }
        dPrice -= (dPrice * departFlight.getDiscount() / 100);
        view.setDepartureInfo(
                departFlight.getDeparting().toString(),
                departFlight.getArriving().toString(),
                departFlight.getDepartureDate(),
                dPrice
        );

        // update ticket price
        this.departTicket.setTotalAmount(dPrice);

        return dPrice;
    }

    public double bindReturnInfo(Booking returnTicket, String ticketClass) {
        assert (returnTicket != null);
        double rPrice = 0.0;
        // round trip ticket
        Flight returnFlight = returnTicket.getFlight();
        if (returnFlight != null) {
            if (returnFlight != null) {
            if (ticketClass.toLowerCase().startsWith("bus")) {
                rPrice = returnFlight.getPrice2();
            } else {
                rPrice = returnFlight.getPrice();
            }
            rPrice -= (rPrice * returnFlight.getDiscount() / 100);
            view.setReturnInfo(
                    returnFlight.getDeparting().toString(),
                    returnFlight.getArriving().toString(),
                    returnFlight.getDepartureDate(),
                    rPrice
                    );
            }
        }

        // update ticket price
        this.returnTicket.setTotalAmount(rPrice);

        return rPrice;
    }

    public void createView() {
        view = new SummaryPanel();
    }

    public Object getView() {
        if (view == null) {
            createView();
        }
        return view;
    }

}
